import { ref } from 'vue'
import type { RequestWrapper } from '@/core/interfaces/common'
import { createRequest } from '@/core/api/request'

// 为了兼容性，定义 BaseResponse 类型别名
type BaseResponse<T> = RequestWrapper<T>

// 接口返回的数据结构定义 - 更新为扁平结构
export interface ComprehensiveAnalysisResponse {
  feedsCount: number // 投放数量
  rankingKeywordCount: number // 排名关键词数量
  playCount: number // 播放数量
  optimizedWordCount: number // 优化词数量
  orderInfo: {
    customerId: string | number
    customerName: string
    createTime: string
  }
  interactionCount: number // 互动数量
  customerCount: number // 客户数量
}

// 请求参数类型
export interface ComprehensiveAnalysisParams {
  orderId: string | number // 经过Base64编码的订单ID
}

const request = createRequest()

export const getComprehensiveAnalysis = (params: ComprehensiveAnalysisParams) => {
  return request<BaseResponse<ComprehensiveAnalysisResponse>>({
    url: '/api/v1/analysis/keyword/comprehensive',
    method: 'GET',
    params
  })
}

// 使用React风格的hook（如果项目需要）
export const useComprehensiveAnalysis = () => {
  const data = ref<ComprehensiveAnalysisResponse | null>(null)
  const loading = ref(false)
  const error = ref<Error | null>(null)

  const setLoading = (state: boolean) => {
    loading.value = state
  }

  const setError = (err: Error | null) => {
    error.value = err
  }

  const setData = (newData: ComprehensiveAnalysisResponse) => {
    data.value = newData
  }

  const execute = async (params: ComprehensiveAnalysisParams) => {
    try {
      setLoading(true)
      setError(null)
      const response = await getComprehensiveAnalysis(params)
      if (response.code === 200 && response.data) {
        setData(response.data)
        return [null, response.data] as const
      } else {
        const err = new Error(response.message || '获取综合分析数据失败')
        setError(err)
        return [err, null] as const
      }
    } catch (err) {
      const error = err as Error
      setError(error)
      return [error, null] as const
    } finally {
      setLoading(false)
    }
  }

  return [data, loading, error, execute] as const
}

// 兼容项目现有风格的API调用方式
export default function useComprehensiveAnalysisAPI() {
  const response = ref<BaseResponse<ComprehensiveAnalysisResponse> | null>(null)
  const loading = ref(false)
  const error = ref<Error | null>(null)

  const setLoading = (state: boolean) => {
    loading.value = state
  }

  const setError = (err: Error | null) => {
    error.value = err
  }

  const setResponse = (res: BaseResponse<ComprehensiveAnalysisResponse>) => {
    response.value = res
  }

  const fetch = async (params: ComprehensiveAnalysisParams) => {
    try {
      setLoading(true)
      setError(null)
      const result = await getComprehensiveAnalysis(params)
      setResponse(result)
      return [null, result] as const
    } catch (err) {
      const error = err as Error
      setError(error)
      return [error, null] as const
    } finally {
      setLoading(false)
    }
  }

  return [response, fetch, loading, error] as const
}
